<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\service\Partner as PartnerService;
use think\Request;

class Partner extends Base
{


    protected $request;
    protected $service;
    protected $validate;
    protected $operator_id;

    public function __construct(Request $request, PartnerService $service)
    {
        $this->request = $request;
        $this->service = $service;
        $this->validate = new \app\admin\validate\Partner();
        $this->operator_id = $request->operator_id;
    }

    /**
     * 显示资源列表
     *
     * @author: Bobo
     * @date: 2024-12-19 18:09:32 
     * @return \think\Response
     */
    public function index()
    {

        $params = empty($this->request->params) ? [] : $this->request->params;

        if (!$this->validate->scene('index')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }

        $pageIndex         = isset($params['pageIndex'])             ? intval($params['pageIndex'])             : 1;
        $pageSize          = isset($params['pageSize'])              ? intval($params['pageSize'])              : 10;
        $name              = isset($params['name'])                  ? $params['name']                          : '';

        $where = ['is_del' => 0];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        $res =  $this->service->getList($pageIndex, $pageSize, $where);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }




    /**
     * 保存新建的资源
     *
     * @author: Bobo
     * @date: 2024-12-20 10:50:26 
     * @return \think\Response
     */
    public function save()
    {
        $params = empty($this->request->params) ? [] : $this->request->params;

        if (!$this->validate->scene('save')->check($params)) {
            return $this->returnJsonData(100001, [], $this->validate->getError());
        }
        $params['creator'] = $this->operator_id;
        $params['create_time'] = date('Y-m-d H:i:s', time());
        $res =  $this->service->save($params);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        if (intval($id) <= 0) {
            return $this->returnJsonData(100001);
        }
        $res =  $this->service->getOne(intval($id));

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $params = empty($this->request->params) ? [] : $this->request->params;
        $params['updater'] = $this->operator_id;
        $params['update_time'] = date('Y-m-d H:i:s', time());
        $where = ['id' => $params['id']];
        $res =  $this->service->update($params, $where);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        if (intval($id) <= 0) {
            return $this->returnJsonData(100001);
        }
        $res =  $this->service->update(['is_del' => time()], ['id' => intval($id)]);

        $code = $res['code'];
        $data = $res['data'] ?? [];
        $message = $res['message'] ?? "";
        return $this->returnJsonData($code, $data, $message);
    }
}
